const { json } = require('body-parser')
const Commit = require('../utils/commit')
const commit = new Commit()
const moment = require('moment')
const Jwt = require('../utils/jwt')
const jwt = new Jwt()
const fs = require('fs')

/**
 * description:用户登录
 *
 * date_time: 2023-04-07 21:27:50
 * @author 冷筱宇 <209103572@qq.com>
 *
*/
exports.login = async (req, res) => {
    const { username, password } = req.body
    if (username && password) {
        const result = await commit.login(username, password)
        if (result) {
            const token = jwt.jwtSign({ username, password })
            return res.json({ code: 200, msg: '登录成功', token })
        } else {
            return res.json({ code: 400, msg: '用户名或密码错误' })
        }
    } else {
        return res.json({ code: 400, msg: '用户名或密码不能为空' })
    }
}

/**
 * description:获取菜单
 *
 * date_time: 2023-04-08 00:16:38
 * @author 冷筱宇 <209103572@qq.com>
*/
exports.menu = async (req, res) => {
    return res.json({ code: 200, data: JSON.parse(fs.readFileSync('./config/menu.json', 'utf-8')) })
}